//
//  ZYTableView.m
//  Merchant
//
//  Created by admin on 16/5/2.
//  Copyright © 2016年 ibendi. All rights reserved.
//

#import "ZYTableView.h"

@implementation ZYTableView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)setCellHeight:(NSInteger)cellHeight
{
    _cellHeight = cellHeight;
    self.tableView.rowHeight = cellHeight;
}
-(NSInteger)page
{
    if (!_page) {
        _page = 0;
    }
    return _page;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) style:UITableViewStylePlain];
        [self addSubview:self.tableView];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        _editingStyle = UITableViewCellEditingStyleNone;
        [self.tableView setExtraCellLineHidden];
         self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        self.resultsSource  = [NSMutableArray array];
        
    }
    return self;
}

-(void)tableviewReloadDataWithData:(NSArray *)dataArray
{
    self.resultsSource = [NSMutableArray arrayWithArray:dataArray];
    [self.tableView reloadData];
}
-(void)tableviewReloadData
{
    [self.tableView reloadData];
}

-(void)zyAddTableViewHeaderView:(void(^)(NSInteger page))refreshingBlock
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 0;
        [self.tableView.mj_footer resetNoMoreData];
        refreshingBlock(self.page);
    }];
}
-(void)zyAddTableViewFooterView:(void(^)(NSInteger page))refreshingBlock
{
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page ++;
        refreshingBlock(self.page);

    }];
}
-(void)zyEndRefreshingWithNoMoreData
{
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
}
-(void)zyFooterEndRefreshing
{
    [self.tableView.mj_footer endRefreshing];
}
-(void)zyHeaderEndRefreshing
{
    [self.tableView.mj_header endRefreshing];

}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    if (self.numberOfSectionsInTableViewCompletion) {
        return self.numberOfSectionsInTableViewCompletion(tableView);
    }else
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (self.numberOfRowsInSectionCompletion) {
        return self.numberOfRowsInSectionCompletion(tableView,section);
    }else
    return [self.resultsSource count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_heightForRowAtIndexPathCompletion) {
        return _heightForRowAtIndexPathCompletion(tableView, indexPath);
    }else
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_cellForRowAtIndexPathCompletion) {

        return _cellForRowAtIndexPathCompletion(tableView, indexPath);
    }else{
        static NSString *CellIdentifier = @"ContactListCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        return cell;
    }
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    if (_canEditRowAtIndexPath) {
        return _canEditRowAtIndexPath(tableView, indexPath);
    }else{
        return NO;
    }
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.editingStyle;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_didSelectRowAtIndexPathCompletion) {
        return _didSelectRowAtIndexPathCompletion(tableView, indexPath);
    }
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (_didDeselectRowAtIndexPathCompletion) {
        _didDeselectRowAtIndexPathCompletion(tableView, indexPath);
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  @"删除";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [tableView beginUpdates];
        [self.resultsSource removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView endUpdates];
        
    }else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}

#pragma mark 代码块实现方法

-(void)zyCellForRowAtIndexPath:(nonnull cellForRowAtIndexPathCompletion)cellCompletion wihtDidSelectRowAtIndexPath:(didSelectRowAtIndexPathCompletion)didCompletion withHeightForRowAtIndexPath:(nonnull heightForRowAtIndexPathCompletion)cellHeightComplection
{
    if (cellCompletion && cellHeightComplection && didCompletion) {
        self.cellForRowAtIndexPathCompletion   = cellCompletion;
        self.heightForRowAtIndexPathCompletion = cellHeightComplection;
        self.didSelectRowAtIndexPathCompletion  = didCompletion;
    }
}


-(void)zyCellForRowAtIndexPath:(cellForRowAtIndexPathCompletion)cellCompletion wihtDidSelectRowAtIndexPath:(didSelectRowAtIndexPathCompletion)didCompletion
{
    if (cellCompletion && didCompletion) {
        self.cellForRowAtIndexPathCompletion   = cellCompletion;
        self.didSelectRowAtIndexPathCompletion  = didCompletion;
    }
}

-(void)zyNumberOfSectionsInTableView:(numberOfSectionsInTableViewCompletion)sectionsCompletion withNumberOfRowsInSection:(numberOfRowsInSectionCompletion)numCompletion
{
    if (sectionsCompletion && numCompletion) {
        self.numberOfSectionsInTableViewCompletion = sectionsCompletion;
        self.numberOfRowsInSectionCompletion = numCompletion;
    }
}
-(void)zyNumberOfSectionsInTableViewCompletion:(numberOfSectionsInTableViewCompletion)completion
{
    if (completion) {
        self.numberOfSectionsInTableViewCompletion = completion;
    }
}
-(void)zyNumberOfRowsInSectionCompletion:(numberOfRowsInSectionCompletion)completion
{
    if (completion) {
        self.numberOfRowsInSectionCompletion  = completion;
    }
}
-(void)zyCellForRowAtIndexPathCompletion:(cellForRowAtIndexPathCompletion)completion
{
    if (completion) {
        self.cellForRowAtIndexPathCompletion = completion;
    }
}
-(void)zyCanEditRowAtIndexPath:(canEditRowAtIndexPath)completion
{
    if (completion) {
        self.canEditRowAtIndexPath = completion;
    }
}

-(void)zyHeightForRowAtIndexPathCompletion:(heightForRowAtIndexPathCompletion)completion
{
    if (completion) {
        self.heightForRowAtIndexPathCompletion =completion;
    }
}
-(void)zyDidSelectRowAtIndexPathCompletion:(didSelectRowAtIndexPathCompletion)completion
{
    if (completion) {
        self.didSelectRowAtIndexPathCompletion =  completion;
    }
}
-(void)zyDidDeselectRowAtIndexPathCompletion:(didDeselectRowAtIndexPathCompletion)completion
{
    if (completion) {
        self.didDeselectRowAtIndexPathCompletion = completion;
    }
}

@end
